import java.util.LinkedList;



public class Rpal {

    private static String _sourceCode;
    private static TreeNode astRoot;
    private static TreeNode stRoot;
    private static Scanner scanner;
    private static Parser parser;
    private static CSEMachine cse;
    private static StConverter stConverter;
    private static LinkedList<Token> tokenList;
    private static String inputPath;
    private static boolean  _AST;
    private static boolean  _TOKENLIST;
    private static boolean  _ST;
    
            
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        //get the file name and command form the parameter passed from cmd
        inputPath = args[args.length - 1];
        
        if(args.length == 0) {
            System.out.print("Command Format: rpal.exe input_path [-token Token-list] [-ast AST] [-file output_path writefile] [-help help]\r\n");
        }
        
        for(int i = 0; i< args.length - 1 && i < args.length; i++) {
            if(args[i].equals("-ast")) {
                _AST = true;
            }
            
            if(args[i].equals("-token")) {
                _TOKENLIST = true;
            }
            
            if(args[i].equals("-st")) {
                _ST = true;
            }
        }
        
        
        //initialization
        scanner = new Scanner();
        parser = new Parser();
        stConverter = new StConverter();
        cse = new CSEMachine();
        
        _sourceCode = Utilities.readFile(inputPath);
        
        scanner.setSource(_sourceCode);
        tokenList = scanner.scan();
        if(_TOKENLIST) {
            for(int i = 0; i < tokenList.size(); i++)
            System.out.print(((Token)tokenList.get(i)).value + "\t");
            System.out.print("\r\n");
        }
        
        parser.setRoot(tokenList);
        astRoot = parser.parse();
        if(_AST) {
            Utilities.traverse(astRoot, 0);
        }

        stConverter.buildST(astRoot);
        stRoot = stConverter.getSt();
        if(_ST) {
            Utilities.traverse(stRoot,0);
        }
        
        cse.setRoot(stRoot);
        cse.evaluate();
    }
}






































